﻿using System;
using System.Collections.Generic;
using System.Text;

public struct Rectangle
{
    public Rectangle(int x,int y,int w,int h)
    {
        this.X = x; this.Y = y;
        this.Width = w; this.Height = h;    
    }
    public int X;
    public int Y;
    public int Width;
    public int Height;
    public void Intersect(Rectangle rect)
    {
        Rectangle result = Intersect(rect, this);

        X = result.X;
        Y = result.Y;
        Width = result.Width;
        Height = result.Height;
    }

    /// <summary>
    /// Creates a rectangle that represents the intersection between a and b. If there is no intersection, an
    /// empty rectangle is returned.
    /// </summary>
    public static Rectangle Intersect(Rectangle a, Rectangle b)
    {
        int x1 = Math.Max(a.X, b.X);
        int x2 = Math.Min(a.X + a.Width, b.X + b.Width);
        int y1 = Math.Max(a.Y, b.Y);
        int y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);

        if (x2 >= x1 && y2 >= y1)
        {
            return new Rectangle(x1, y1, x2 - x1, y2 - y1);
        }

        return new Rectangle(0, 0, 0, 0);
    }

}
public struct RectangleF
{
    public RectangleF(float x, float y, float w, float h)
    {
        this.X = x; this.Y = y;
        this.Width = w; this.Height = h;
    }
    public float X;
    public float Y;
    public float Width;
    public float Height;

    public void Intersect(RectangleF rect)
    {
        RectangleF result = Intersect(rect, this);

        X = result.X;
        Y = result.Y;
        Width = result.Width;
        Height = result.Height;
    }

    /// <summary>
    /// Creates a rectangle that represents the intersection between a and b. If there is no intersection, an
    /// empty rectangle is returned.
    /// </summary>
    public static RectangleF Intersect(RectangleF a, RectangleF b)
    {
        float x1 = Math.Max(a.X, b.X);
        float x2 = Math.Min(a.X + a.Width, b.X + b.Width);
        float y1 = Math.Max(a.Y, b.Y);
        float y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);

        if (x2 >= x1 && y2 >= y1)
        {
            return new RectangleF(x1, y1, x2 - x1, y2 - y1);
        }

        return new RectangleF(0, 0, 0, 0);
    }
}